<?php

namespace app\controller;

use support\Request;
use app\service\Catalog as CatalogService;

class Catalog
{

    private $catalog;

    public function __construct(CatalogService $catalog)
    {
        $this->catalog = $catalog;
    }

    /**
     * 创建目录
     * @param Request $request
     * @return \support\Response
     */
    public function create(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $res = $this->catalog->create($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 更新目录
     * @param Request $request
     * @return \support\Response
     */
    public function update(Request $request)
    {
        $param = $request->all();
        $param['data']['created_by'] = $request->getCurrentUserId();
        $res = $this->catalog->update($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 删除目录
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        $res = $this->catalog->delete($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * @param Request $request
     * @return \support\Response
     * @throws \exception
     */
    public function select(Request $request)
    {
        $param = $request->all();
        $res = $this->catalog->select($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 查询目录
     * @param Request $request
     * @return \support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findByName(Request $request)
    {
        $param = $request->all();
        $res = $this->catalog->findByName($param['param']);
        return json(success_response('', $res));
    }
}
